package com.community.app.controller;


import com.community.body.domain.ZyComment;
import com.community.body.domain.ZyCommunityInteraction;
import com.community.body.domain.ZyFiles;
import com.community.body.dto.CommentDTO;
import com.community.body.dto.InteractionDTO;
import com.community.body.dto.OwnerCommunityDTO;
import com.community.body.dto.ZyCommunityDTO;
import com.community.body.service.IZyCommentService;
import com.community.body.service.IZyCommunityInteractionService;
import com.community.body.service.IZyCommunityService;
import com.community.body.service.IZyFilesService;
import com.community.body.vo.ZyCommentVO;
import com.community.body.vo.ZyCommunityInteractionVO;
import com.community.common.core.domain.AjaxResult;
import com.community.common.core.domain.entity.SysDictData;
import com.community.common.utils.file.FileUtils;
import com.community.common.utils.oss.AliyunOssUploadUtils;
import com.community.system.service.ISysDictDataService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.community.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/mini/community/wx/interaction")
public class WxCommunityInteractionController {

    @Resource
    private IZyCommunityInteractionService zyCommunityInteractionService;
    @Resource
    private IZyCommunityService communityService;
    @Resource
    private RedisTemplate<String, Long> redisTemplate;
    @Resource
    private IZyFilesService zyFilesService;
    @Resource
    private IZyCommentService commentService;
    @Resource
    private ISysDictDataService dictDataService;


    /**
     * 查询评论列表
     */
    @GetMapping("commentList")
    public AjaxResult list(ZyComment zyComment) {
        List<CommentDTO> list = commentService.selectZyCommentList(zyComment);
        return AjaxResult.success(list);
    }

    /**
     * 查询帖子列表
     */
    @GetMapping("list")
    public AjaxResult list(InteractionDTO interactionDTO) {
        startPage();
        List<InteractionDTO> list = zyCommunityInteractionService.selectZyCommunityInteractionList(interactionDTO);
        return AjaxResult.success(list);
    }

    /**
     * 热帖
     *
     * @param interactionDTO
     * @return
     */
    @GetMapping("selectHotZyCommunityInteractionList")
    public AjaxResult selectHotZyCommunityInteractionList(InteractionDTO interactionDTO) {
        return AjaxResult.success(zyCommunityInteractionService.selectHotZyCommunityInteractionList(interactionDTO));
    }

    /**
     * 查询绑定状态
     *
     * @param dictData
     * @return
     */
    @GetMapping("status")
    public AjaxResult getStatus(SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return AjaxResult.success(list);
    }

    /**
     * 删除帖子
     */
    @RequestMapping("deleteCommunityInteraction")
    public AjaxResult updateZyCommunityInteraction(ZyCommunityInteraction zyCommunityInteraction) {
        int i = zyCommunityInteractionService.updateZyCommunityInteraction(zyCommunityInteraction);
        return i > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }

    /**
     * 删除评论
     */
    @RequestMapping("deleteZyComment")
    public AjaxResult updateZyComment(ZyComment zyComment) {
        int i = commentService.updateZyComment(zyComment);
        return i > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }


    /**
     * 查询业主是否有房子
     */
    @RequestMapping("selectCommunityList")
    public AjaxResult selectZyCommunityList3(ZyCommunityDTO zyCommunityDTO) {
        return AjaxResult.success(communityService.selectZyCommunityList3(zyCommunityDTO));
    }


    /**
     * 查询业主在该小区的房子
     */
    @RequestMapping("selectCommunityOwnerList")
    public AjaxResult selectCommunityList5(OwnerCommunityDTO ownerCommunityDTO) {
        return AjaxResult.success(communityService.selectZyCommunityList5(ownerCommunityDTO));
    }

    /**
     * 上传图片
     */
    @RequestMapping("/common/upload")
    public AjaxResult insertImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        Long owner = Long.valueOf(request.getHeader("owner"));
        if (!file.isEmpty()) {
            String url = AliyunOssUploadUtils.uploadFile(file);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", FileUtils.getName(url));
            ajax.put("newFileName", FileUtils.getName(url));
            ajax.put("originalFilename", file.getOriginalFilename());
            ZyFiles zyFiles = new ZyFiles();
            zyFiles.setFilesUrl(url);
            zyFiles.setDelFlag(0L);
            zyFiles.setCreateTime(new Date());
            zyFiles.setUserId(owner);
            zyFiles.setRemark("zyCommunityInteraction");
            ValueOperations<String, Long> value = redisTemplate.opsForValue();
            Long s = value.get("communityInteractionId");
            zyFiles.setParentId(s);
            zyFiles.setSource(0L);
            zyFilesService.insertZyFiles(zyFiles);
            return AjaxResult.success("上传成功", ajax);
        }
        return AjaxResult.error("上传图片异常，请联系管理员");
    }

    /**
     * 新增帖子
     *
     * @return
     */
    @RequestMapping("insertZyCommunityInteraction")
    public AjaxResult InsertZyCommunityInteraction(@RequestBody ZyCommunityInteractionVO zyCommunityInteraction) {
        int i = zyCommunityInteractionService.insertZyCommunityInteraction(zyCommunityInteraction);
        return i > 0 ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }

    /**
     * 新增评论
     *
     * @param zyComment
     * @return
     */
    @RequestMapping("insertComment")
    public AjaxResult InsertComment(@RequestBody ZyCommentVO zyComment) {
        int i = commentService.insertZyComment(zyComment);
        return i > 0 ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }


}
